/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import entities.Resposta;
import entities.Server;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.ConfigGeral;
import traffic.ControladorTrafego;
import static traffic.ControladorTrafego.clientID;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class RmiClientApp {

    private static boolean SHOW_ERROR = false;

    RmiAppInterface rmiServer;
    Registry registry;
    String serverAddress = ConfigGeral.CG_SERVER_APP_IP;
    int serverPort = ConfigGeral.CG_SERVER_APP_PORT;
    String serverName = ConfigGeral.CG_SERVER_APP_NAME;

    public Resposta listaTeatros() {

        Resposta resposta = null;

        if (!parteComum()) {
            return null;
        }

        try {
            resposta = rmiServer.listTheaters();
        } catch (RemoteException ex) {
            if (SHOW_ERROR) {
                Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

        return resposta;
    }

    public Resposta getTeatro(String selected) {
        Resposta resposta = null;

        if (!parteComum()) {
            return null;
        }

        try {
            resposta = rmiServer.getTheater(selected);
        } catch (RemoteException ex) {
            if (SHOW_ERROR) {
                Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

        return resposta;
    }

    //(String theaterName, int lugarID, int userID)
    public Resposta reserveSeat(String theaterName, int lugarID, int userID) {
        Resposta resposta = null;

        if (!parteComum()) {
            return null;
        }

        try {
            resposta = rmiServer.reserveSeat(theaterName, lugarID, userID);
        } catch (RemoteException ex) {
            if (SHOW_ERROR) {
                Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("deu erro");
            }
            return null;
        }

        return resposta;
    }

    public Resposta purchase() {
        Resposta resposta = null;

        if (!parteComum()) {
            return null;
        }

        int clientID = 0;
        Random rand = new Random();

        if (ControladorTrafego.randomClients) {

            clientID = rand.nextInt(100000);
        } else {
            clientID = ControladorTrafego.clientID;
        }

        String selectedTeatro = null;

        int tNum = rand.nextInt(ControladorTrafego.numTeatros);
        String teatroNome = "teatro" + tNum;

        int lNum = ControladorTrafego.numRows * ControladorTrafego.numCols;

        try {
            resposta = rmiServer.purchaseSeat(teatroNome, lNum, clientID);
        } catch (RemoteException ex) {
            if (SHOW_ERROR) {
                Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("deu erro");
            }
            return null;
        }

        return resposta;
    }

    private boolean parteComum() {

        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress, serverPort);

            // look up the remote object
            rmiServer = (RmiAppInterface) (registry.lookup(serverName));

        } catch (RemoteException e) {
            if (SHOW_ERROR) {
                e.printStackTrace();
            }
            return false;
        } catch (NotBoundException e) {
            if (SHOW_ERROR) {
                e.printStackTrace();
            }
            return false;
        }
        return true;

    }

    public void selectedServer(Server server) {
        serverAddress = server.getIp();
        serverPort = server.getPort();
        //serverName = server.getRmiBindName();
    }

}
